package finalproject.bd1.sql.gui;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import javax.swing.JComboBox;


public class FormServiceGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField nameField;
	private JTextField costField;
	private JLabel lblCadastroDeClientes;
	private JTextField serviceField;
	private JLabel lblCategory;
	private JLabel lblDescriao;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater( new Runnable() {
			public void run() {
				try {
					FormServiceGUI frame = new FormServiceGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FormServiceGUI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 449, 385);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setTitle("Cadastro de servi\u00E7os");
		setLocationRelativeTo(null);
		contentPane.setLayout(null);
		
		fieldsForm(); //Call all itens form.

		buttons(); //Call buttons to write data.
	}

	/**
	 * Call all itens form.
	 */
	private void fieldsForm() {
		lblCadastroDeClientes = new JLabel("Cadastro de servi\u00E7os");
		lblCadastroDeClientes.setHorizontalAlignment(SwingConstants.CENTER);
		lblCadastroDeClientes.setFont(new Font("Times New Roman", Font.BOLD, 18));
		lblCadastroDeClientes.setBounds(118, 21, 196, 14);
		contentPane.add(lblCadastroDeClientes);
		
		JLabel lblName = new JLabel("C\u00F3digo:");
		lblName.setBounds(10, 64, 49, 14);
		contentPane.add(lblName);
		
		nameField = new JTextField();
		nameField.setBounds(94, 61, 220, 20);
		contentPane.add(nameField);
		nameField.setColumns(10);
		setName(nameField.getText());
		
		lblCategory = new JLabel("Categoria:");
		lblCategory.setBounds(10, 92, 77, 14);
		contentPane.add(lblCategory);
		
		JComboBox categoryComboBox = new JComboBox();
		categoryComboBox.setBounds(94, 89, 220, 20);
		contentPane.add(categoryComboBox);
		
		JLabel lblService = new JLabel("Nome Servi\u00E7o: ");
		lblService.setBounds(10, 120, 98, 14);
		contentPane.add(lblService);
		
		serviceField = new JTextField();
		serviceField.setColumns(10);
		serviceField.setBounds(94, 117, 329, 20);
		contentPane.add(serviceField);
		
		lblDescriao = new JLabel("Descri\u00E7ao: ");
		lblDescriao.setBounds(10, 145, 77, 14);
		contentPane.add(lblDescriao);
		
		JTextArea descriptionArea = new JTextArea();
		descriptionArea.setBounds(94, 145, 329, 122);
		contentPane.add(descriptionArea);
		
		JLabel lblCost = new JLabel("Valor(R$)");
		lblCost.setBounds(10, 281, 59, 14);
		contentPane.add(lblCost);
		
		costField = new JTextField();
		costField.setBounds(94, 278, 133, 20);
		contentPane.add(costField);
		costField.setColumns(10);
	}
	
	/**
	 * Call buttons to write data.
	 */
	private void buttons() {
		JButton btnCadastraCliente = new JButton("Cadastrar");
		btnCadastraCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, nameField.getText());
			}
		});
		btnCadastraCliente.setForeground(Color.BLACK);
		btnCadastraCliente.setBounds(158, 313, 117, 23);
		contentPane.add(btnCadastraCliente);

		JButton btnCancelar = new JButton("Limpar");
		btnCancelar.addActionListener(new ActionListener() {
			/**
			 * Clean all fields in frame
			 * @author Grinvon
			 * @link http://www.guj.com.br/java/32502-limpando-todos-os-campos-jtextfield#174173
			 */
			public void actionPerformed(ActionEvent event) {
				for (int i=0; i < getContentPane().getComponentCount(); i++) {
					//Mapping each component on Jframe.
					Component c = getContentPane().getComponent(i);
					if (c instanceof JTextField) {
						JTextField field = (JTextField) c;
						field.setText("");
						System.out.println("Cleaning field " + i);
					}
				}
			}
		});
		btnCancelar.setForeground(Color.BLACK);
		btnCancelar.setBounds(337, 313, 86, 23);
		contentPane.add(btnCancelar);
	}
}
